<?php
namespace app\admin\controller;

use think\Request;

class Coupon extends Common
{
    //优惠券首页
    public function index()
    {
        //升序排列
        $couponlist = db('coupon')->order('id desc')->paginate(10);
        $page=$couponlist->render();
        //dump($page);die;
        $page = str_replace(
            ["pagination", 
            "disabled",
            "active"],
        ["am-pagination tpl-pagination",
        "am-disabled",
        "am-active"],
        $page);
        //echo $page;die;
        $this->assign('couponlist' , $couponlist);
        //dump($couponlist);die;
        $this->assign('page' , $page);
        return view();
    }
    //优惠券添加页面
    public function add()
    {
        return view();
    }
    //点击改变优惠券推荐状态
    public function get_status($coupon_id){

        $ret = db('coupon')->where("id = '$coupon_id'")->find();
        if($ret['status']==1){
            db('coupon')->where("id = '$coupon_id'")->update(['status'=>'0']);
        }else{
            db('coupon')->where("id = '$coupon_id'")->update(['status'=>'1']);
        }
        $ret = db('coupon')->where("id = '$coupon_id'")->find();
        return $ret['status'];
    }

    //新增添加入库
    public function add_coupon(){
        $data = input('post.');
        $send_start_time=input('post.send_start_time');
        $send_start_time=explode('-', $send_start_time);
        $send_start_time=mktime($send_start_time[0],$send_start_time[1],$send_start_time[2]);
        // dump($send_start_time);die;
        $send_end_time=input('post.send_end_time');
        $send_end_time=explode('-', $send_end_time);
        $send_end_time=mktime($send_end_time[0],$send_end_time[1],$send_end_time[2]);

        $use_start_time=input('post.use_start_time');
        $use_start_time=explode('-', $use_start_time);
        $use_start_time=mktime($use_start_time[0],$use_start_time[1],$use_start_time[2]);


        $use_end_time=input('post.use_end_time');
        $use_end_time=explode('-', $use_end_time);
        $use_end_time=mktime($use_end_time[0],$use_end_time[1],$use_end_time[2]);

         //dump($data);die;
         $validate = validate('Coupon');
         if(!$validate->check($data)){
             $this->error($validate->getError());
         }
         if($send_start_time>$send_end_time){
            $this->error('优惠券发送开始日期必须在发放截止日期之前');
         }
         if($use_start_time>$use_end_time){
            $this->error('优惠券使用开始日期必须在使用截止日期之前');
         }
         
        $ret = db('coupon')->insert($data);
        if($ret){
            $this->success('添加成功','admin/coupon/index');
        }else{
            $this->error('添加失败');
        }
    }
    //优惠券编辑页面
    public function edit()
    {
        $id=input('param.id');
        $data = input('post.');
        $result = db('coupon')->where("id=$id")->find();
        //dump($result);die;
        $this->assign('result',$result);
        return view();
    }

    //改优惠券
    public function update()
    {
        $id=input('param.id');
        $data=input('post.');
        $result = db('coupon')->where("id=$id")->find();
        //dump($result);dump($data);die;
        $send_start_time=input('post.send_start_time');
        $send_start_time=explode('-', $send_start_time);
        $send_start_time=mktime($send_start_time[0],$send_start_time[1],$send_start_time[2]);
        // dump($send_start_time);die;
        $send_end_time=input('post.send_end_time');
        $send_end_time=explode('-', $send_end_time);
        $send_end_time=mktime($send_end_time[0],$send_end_time[1],$send_end_time[2]);

        $use_start_time=input('post.use_start_time');
        $use_start_time=explode('-', $use_start_time);
        $use_start_time=mktime($use_start_time[0],$use_start_time[1],$use_start_time[2]);

        $use_end_time=input('post.use_end_time');
        $use_end_time=explode('-', $use_end_time);
        $use_end_time=mktime($use_end_time[0],$use_end_time[1],$use_end_time[2]);

        $validate = validate('Coupon');
         if(!$validate->check($data)){
             $this->error($validate->getError());
         }
         if($send_start_time>$send_end_time){
            $this->error('优惠券发送开始日期必须在发放截止日期之前');
         }
         if($use_start_time>$use_end_time){
            $this->error('优惠券使用开始日期必须在使用截止日期之前');
         }
        
        $ret=db('coupon')->where("id=$id")->update($data);
        if($ret == false){
            $this->error('修改失败');
        }else{
            $this->success('修改成功','index');
        }
    }

    //删除优惠券
    public function delete()
    {
        $id=$_GET['id'];
        $ret=db('coupon')->where("id=$id")->delete();
        if($ret){
            $this->success("删除成功!");
        }else{
            $this->error("删除失败");
        }
    }

    //推荐优惠券
    public function tuijian()
    {
        $id = input('param.id');
        $status = input('param.status');
        //0为推荐
        //1为不推荐
        //setField数据库更新部分数据
        if($status==0){
            $result=db('coupon')->where("id=$id")->setField('status', 1);
        }
        if($status==1){
            $result=db('coupon')->where("id=$id")->setField('status', 0);
        }
        $this->redirect('admin/coupon/index');
    }
}
